<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Service\SiteService;
use Illuminate\Support\Facades\View;

class ZhController extends Controller
{
    protected $template = '';

    protected $service = null;

    public function __construct()
    {
        $this->template = env('APP_TEMPLATE', '');

        $this->service = new SiteService();

        View::share('navs', $this->service->getNavs());

        $navsList = $this->service->getNavs()->each(function ($item) {
            $item->children = $this->service->categoryBy($item->id);
            $item->articles = $this->service->recommend($item->id)->take(8);
        });
        View::share('navsList', $navsList);

        View::share('configs', $this->service->getConfigs());

        View::share('tagsList', $this->service->getTags());
    }



    public function index()
    {
        $bannerList = $this->service->getBanners();

        $navs = $this->service->homeNavs();
        $modules = $navs->each(function ($item) {
            $item->categorys = collect($this->service->categoryBy($item->id))->each(function ($item) {
                $item->items = $this->service->homeArticleList($item->id);
            });
            $item->articles = $this->service->moduleArticleList($item->id);
        });
        // return $this->successful($modules);

        $recommendList = $this->service->recommend(null, 8);

        View::share(compact('bannerList', 'modules', 'recommendList'));
        return view($this->template . 'index');
    }

    public function content($id)
    {
        $type = $id;
        $tag = request('tag', '');
        $categoryId = request('category_id', '');

        if (request()->isMethod('post')) {
            $recommend = request()->has('recommend');
            if ($recommend) {
                $list = $this->service->recommend($type, 8);
                return $this->successful($list);
            }

            $list = $this->service->list($type, $categoryId, $tag);
            return $this->successful($list);
        }

        $nav = $this->service->getNavs()->filter(function ($item) use ($type) {
            return $item->id == $type;
        })->first();
        $nav->adv = $this->service->getAdvs()[$type] ?? '';

        $categoryList = $this->service->categoryBy($type);

        $list = $this->service->list($type, $categoryId, $tag);

        $recommendList = $this->service->recommend($type);
        return view($this->template . 'content', compact(
            'id',
            'nav',
            'categoryList',
            'list',
            'recommendList'
        ));
    }

    public function article($id)
    {
        $type = request('type', 0);
        $info = $this->service->detail($id, $type);
        if ($info) {
            // 设置文章 SEO
            $configs = $this->service->getConfigs();
            $configs['seo.keywords'] = $info->title;
            $configs['seo.description'] = $info->tags;
            View::share('configs', $configs);
            // return $this->successful($info);

            $list = $this->service->recommend($info->type);
            return view($this->template . 'show', compact('info', 'list'));
        }
        return redirect()->back();
    }


    protected function hasTemplate($name)
    {
        if (!view()->exists($name)) {
            // 视图模版不存在，进入首页路由
            // return redirect('/');
            // 视图模版不存在，返回上一步
            return redirect()->back();
        }
        return view($name);
    }

    public function single($name)
    {
        return $this->hasTemplate($this->template . $name);
    }
}
